<template>
  <div class="content flex">
    <!-- 左侧固定导航 -->
    <StudentMenu />

    <!-- 右侧内容区 -->
    <main class="ml-64 flex-1 p-6 bg-gray-50 min-h-screen">
      <el-card shadow="never" style="margin-bottom: 10px">
        <header class="flex justify-between items-center mb-6">
          <div>
            <h1 class="text-3xl font-bold text-gray-800">我的老师</h1>
            <p class="text-sm text-gray-500 mt-1">就业负责老师: {{ FZJS.jsmc }}</p>
          </div>
          <div class="flex items-center space-x-4">
            <!--            <el-button icon="el-icon-plus" type="text" @click="addGw">审核通过</el-button>-->
            <!--            <el-button icon="el-icon-plus" type="text" @click="addGw">发布新岗位</el-button>
                        <el-button icon="el-icon-edit" type="text" @click="eiditGw">编辑岗位</el-button>-->
          </div>
        </header>
      </el-card>

      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <div
            v-for="teacher in teachers"
            :key="teacher.id"
            :class="['bg-white p-6 rounded-xl  border transition', {
            'border-green-500': teacher.id === responsibleTeacherId
          }]"
        >
          <div class="mb-4">
            <h2 class="text-xl font-semibold">{{ teacher.jsmc }}</h2>
<!--            <p class="text-gray-500 mt-1">电话：{{ teacher.phone }}</p>-->
<!--            <p class="text-gray-500">邮箱：{{ teacher.email }}</p>-->
          </div>
          <div class="flex space-x-4 mt-4">
            <el-button type="default" size="mini" @click="contactTeacher(teacher)"  >联系</el-button>
            <el-button type="primary" v-if="teacher.id!== FZJS.id" @click="setFzjs(teacher)" size="mini" >设为负责老师</el-button>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import StudentMenu from "@/components/student/Student_menu.vue";
import axios from "axios";
export default {
  name: "MyTeacher",
  components: { StudentMenu },
  data() {
    return {
      responsibleTeacherId: null,
      teachers: [],
      UserInfo: {},
      FZJS:{},
    };
  },
  mounted() {
    this.getLoginUserInfo();
  },
  methods: {
    getFZJS(){
      axios.get("/teacher/getChargeTeacher?stuid="+this.UserInfo.id).then((response) => {
        if (response.data.result) {
          this.FZJS = response.data.data;
        }else{
          this.$message.error("获取负责老师信息失败:"+response.data.msg);
        }
      }).catch((error) => {
        console.error("获取负责老师信息失败！", error);
        this.$message.error("获取负责老师信息失败:"+error.message);
      });
    },
    getJsdmk(){
      axios.get(("/teacher/getTeacherByStuid?stuid="+this.UserInfo.id)).then((response) => {
        if (response.data.result) {
            this.teachers = response.data.data;
        }else{
          this.$message.error("获取教师信息失败:"+response.data.msg);
        }
      }).catch((error) => {
        console.error("获取教师信息失败！", error);
        this.$message.error("获取教师信息失败:"+error.message);
      });
    },
    async getLoginUserInfo() {
      try {
        this.loading = true;
        const response = await axios.get('/user/checkSession');
        if (!response.data.result) {
          this.handleAuthFailure(response.data.msg);
        } else {
          this.UserInfo = {
            id:response.data.YHID,
            name: response.data.name,
            role: response.data.role,
            username: response.data.username
          };
          this.getJsdmk();
          this.getFZJS();
        }
      } catch (error) {
        console.error('获取用户信息失败，网络错误！', error);
        this.handleAuthFailure();
      }
    },
    setFzjs(teacher){
      this.$confirm('确定设为负责老师吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let StudentData=new FormData();
        StudentData.append("id",this.UserInfo.id);
        StudentData.append("fzjs",teacher.id);

        axios.post("/student/updateStudentByEdit",StudentData).then((response) => {
          if (response.data.result) {
            this.$message.success("设置负责老师成功！");
            this.getJsdmk();
            this.FZJS = teacher;
          }else{
            this.$message.error("设置负责老师失败:"+response.data.msg);
          }
        }).catch((error) => {
          this.$message.error("设置负责老师失败:"+error.message);
        });
        })
    },
    // 添加联系人
    contactTeacher(teacher) {
      let postData = new FormData();
      postData.append("fromyhm", this.UserInfo.username);// 本人
      postData.append("toyhm", teacher.dlzh);// 教师
      postData.append("fromyhsfdm", 4); // 学生
      postData.append("toyhsfdm", 2);
      postData.append("nr", "老师您好，我是学生:："+this.UserInfo.name);
      axios.post("/sstx/addNewlxr", postData).then(response => {
        if (response.data.result) {
          this.$message.success("添加联系人成功！");
          console.log(response.data.data);
          // 跳转到实时聊天界面
          this.$router.push({
            name: 'StudentXxlView',
            params: {lyyhm: response.data.data.lyyhm, id: response.data.data.id}
          });
        } else {
          this.$message.error("添加联系人失败：" + response.data.msg);
        }
      }).catch(error => {
        console.log(error);
        this.$message.error("添加联系人失败：" + error.msg);
      });
    },
  }
};
</script>

<style scoped>
.content {
  display: flex;
}
</style>
